package Solution101_200.problem112.PathSum;

import common.TreeNode;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/07/12 15:52
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        return sumValue(root, 0, sum);
    }

    private boolean sumValue(TreeNode root, int temp,int sum) {

        temp = temp + root.val;
        if (root.left == null && root.right == null) {
            /*找到就返回，不继续计算了，这样能节省时间*/
            return sum == temp;
        } else if (root.left == null) {
            return sumValue(root.right, temp,sum);
        } else if (root.right == null) {
            return sumValue(root.left, temp,sum);
        } else {
            return sumValue(root.right, temp,sum)||sumValue(root.left, temp,sum);
        }
    }
}
